package ControlLayer;
import ModelLayer.*;
import DBLayer.*;

import java.util.ArrayList;
/**
 *
 * @author Pavel
 * 
 */

public class ProductCtr
{
    public ProductCtr()
    {}
    
    public ArrayList<GunReplicas> getAllGunReplicas()
    {
    	IFDBGunReplicas dbGunReplicas=new DBGunReplica();
        ArrayList<GunReplicas> allGunReplicas = new ArrayList<GunReplicas>();
        allGunReplicas=dbGunReplicas.getAllGunReplicas(false);
        return allGunReplicas;
    }
    
    public ArrayList<Clothing> getAllClothing()
    {
    	IFDBClothing dbClothing=new DBClothing();
    	ArrayList<Clothing> allClothing = new ArrayList<Clothing>();
    	allClothing=dbClothing.getAllClothing(false);
    	return allClothing;
    }
    
    public ArrayList<Equipment> getAllEquipment()
    {
    	IFDBEquipment dbEquipment = new DBEquipment();
    	ArrayList<Equipment> allEquipment=new ArrayList<Equipment>();
    	allEquipment=dbEquipment.getAllEquipment(false);
    	return allEquipment;
    }
    
    public ArrayList<Product> getAllSuppProd(int suppId)
    {
    	IFDBProduct dbProduct=new DBProduct();
    	ArrayList<Product> suppProdList=new ArrayList<Product>();
    	suppProdList=dbProduct.getProductsBySuppID(suppId, false);
    	return suppProdList;
    }
    
    public GunReplicas searchGunReplicaById(int id)
    {
    	IFDBGunReplicas dbGunReplica=new DBGunReplica();
    	return dbGunReplica.getGunReplicaByID(id, true);
    }
    
    public Clothing searchClothingByID(int id)
    {
    	IFDBClothing dbClothing=new DBClothing();
    	return dbClothing.findClothingByID(id, true);
    }
    
    public Equipment searchEquipmentByID(int id)
    {
    	IFDBEquipment dbEquipment=new DBEquipment();
    	return dbEquipment.getEquipmentByID(id, true);
    }
    
    public Product searchByName(String name)
    {
    	IFDBProduct dbProduct=new DBProduct();
    	return dbProduct.getProductByName(name, true);
    }
    
    public void insertEquipment(String name, int suppID, float purchasePrice,
			float salesPrice, float rentPrice, String countryOfOrigin, int minStock,
			String newType, String newDescription)
    {
    	Equipment newEquipment=new Equipment();
    	newEquipment.setCountryOfOrigin(countryOfOrigin);
    	newEquipment.setDescription(newDescription);
    	newEquipment.setMinStock(minStock);
    	newEquipment.setName(name);
    	newEquipment.setPurchasePrice(purchasePrice);
    	newEquipment.setRentPrice(rentPrice);
    	newEquipment.setSalesPrice(salesPrice);
    	newEquipment.setType(newType);
    	
    	Supplier suppObj=new Supplier();
    	IFDBSupplier dbSupplier=new DBSupplier();
    	suppObj=dbSupplier.getSuppById(suppID, false);
    	
    	newEquipment.setSupplier(suppObj);
    	
    	try
		{
			 DBConnection1.startTransaction();
			 DBEquipment dbEquipment=new DBEquipment();
			 dbEquipment.insertEquipment(newEquipment);
			 DBConnection1.commitTransaction();
		}
    	catch(Exception e)
    	{
    		DBConnection1.rollbackTransaction();
    	}
    }
    
    public void insertClothing(String name, int suppID, float purchasePrice,
			float salesPrice, float rentPrice, String countryOfOrigin, int minStock,
			String newColour,String newSize)
    {
    	Supplier suppObj=new Supplier();
    	IFDBSupplier dbSupplier=new DBSupplier();
    	suppObj=dbSupplier.getSuppById(suppID, false);
    	
    	Clothing newClothing=new Clothing();
    	newClothing.setColour(newColour);
    	newClothing.setCountryOfOrigin(countryOfOrigin);
    	newClothing.setMinStock(minStock);
    	newClothing.setName(name);
    	newClothing.setPurchasePrice(purchasePrice);
    	newClothing.setRentPrice(rentPrice);
    	newClothing.setSalesPrice(salesPrice);
    	newClothing.setSize(newSize);
    	newClothing.setSupplier(suppObj);
    	
    	try
		 {
			 DBConnection1.startTransaction();
			 DBClothing dbClothing=new DBClothing();
			 dbClothing.insertClothing(newClothing);
			 DBConnection1.commitTransaction();
		 }
		 catch(Exception e)
		 {
			 DBConnection1.rollbackTransaction();
		 }
    }
    
    public void insertGunReplicas(String name, int suppID, float purchasePrice,
			float salesPrice, float rentPrice, String countryOfOrigin, int minStock,
			String newFabric,float newCalibre)
    {
    	Supplier suppObj=new Supplier();
    	IFDBSupplier dbSupplier=new DBSupplier();
    	suppObj=dbSupplier.getSuppById(suppID, false);
    	
    	GunReplicas newReplica=new GunReplicas();
    	newReplica.setCalibre(newCalibre);
    	newReplica.setCountryOfOrigin(countryOfOrigin);
    	newReplica.setFabric(newFabric);
    	newReplica.setMinStock(minStock);
    	newReplica.setName(name);
    	newReplica.setPurchasePrice(purchasePrice);
    	newReplica.setRentPrice(rentPrice);
    	newReplica.setSalesPrice(salesPrice);
    	newReplica.setSupplier(suppObj);
    	
    	try
		 {
			 DBConnection1.startTransaction();
			 DBGunReplica dbReplicas=new DBGunReplica();
			 dbReplicas.insertGunReplica(newReplica);
			 DBConnection1.commitTransaction();
		 }
		 catch(Exception e)
		 {
			 DBConnection1.rollbackTransaction();
		 }
    }
    
    public int updateEquipmentByID(String newType, String newDescription)
    {
    	IFDBEquipment dbEquipment=new DBEquipment();
    	
    	Equipment newEquipment=new Equipment();
    	newEquipment.setType(newType);
    	newEquipment.setDescription(newDescription);
    	
    	return dbEquipment.updateEquipmentByID(newEquipment);
    }
    
    public int updateClothingByID(String newColour,String newSize)
    {
    	IFDBClothing dbClothing=new DBClothing();
    	
    	Clothing newClothing=new Clothing();
    	newClothing.setColour(newColour);
    	newClothing.setSize(newSize);
    	
    	return dbClothing.updateClothing(newClothing);
    }
    
    public int updateGunReplicaByID(String newFabric,float newCalibre)
    {
    	IFDBGunReplicas dbReplicas=new DBGunReplica();
    	
    	GunReplicas newReplica=new GunReplicas();
    	newReplica.setFabric(newFabric);
    	newReplica.setCalibre(newCalibre);
    	
    	return dbReplicas.updateGunReplicaByID(newReplica);
    }
    
    public int updateProductByID(int ProdID, String Name, float PurchasePrice, float SalesPrice, float RentPrice, 
    		String CoutntryOfOrigin, int MinStock, int SuppID)
    {
    	IFDBProduct dbProduct=new DBProduct();
    	
    	Product newProduct=new Product();
    	newProduct.setCountry(CoutntryOfOrigin);
    	newProduct.setId(ProdID);
    	newProduct.setMinStock(MinStock);
    	newProduct.setName(CoutntryOfOrigin);
    	newProduct.setPurchasePrice(SalesPrice);
    	newProduct.setRentPrice(RentPrice);
    	newProduct.setSalesPrice(SalesPrice);
    	
    	return dbProduct.updateProductByID(newProduct);
    }
    
    public int updateProductByName(String Name, float PurchasePrice, float SalesPrice, float RentPrice, String CoutntryOfOrigin, int MinStock, int SuppID)
    {
    	IFDBProduct dbProduct=new DBProduct();
    	
    	Product newProduct=new Product();
    	newProduct.setCountry(CoutntryOfOrigin);
    	//newProduct.setId(ProdID);
    	newProduct.setMinStock(MinStock);
    	newProduct.setName(CoutntryOfOrigin);
    	newProduct.setPurchasePrice(SalesPrice);
    	newProduct.setRentPrice(RentPrice);
    	newProduct.setSalesPrice(SalesPrice);
    	
    	return dbProduct.updateProductByName(newProduct);
    }
    
    public int deleteEquipmentByID(int id)
    {
    	IFDBEquipment dbEquipment=new DBEquipment();
    	return dbEquipment.deleteEquipmentByID(id);
    }
    
    public int deleteClothingByID(int id)
    {
    	IFDBClothing dbClothing=new DBClothing();
    	return dbClothing.deleteClothing(id);
    }
    
    public int deleteGunReplica(int id)
    {
    	IFDBGunReplicas dbGunReplicas= new DBGunReplica();
    	return dbGunReplicas.deleteGunReplicaByID(id);
    }
    
    public int deleteProductByID(int id)
    {
    	IFDBProduct dbProd=new DBProduct();
    	return dbProd.deleteProductByID(id);
    }
    
    public int deleteProductByName(String name)
    {
    	IFDBProduct dbProd=new DBProduct();
    	return dbProd.deleteProductByName(name);
    }
}